package com.disktalk.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class EKPAuth {
	 private static final String LOAD_MD5_PASSWORD_Oracle = "SELECT t.fd_password FROM system.sys_org_person t left join system.sys_org_element e on t.fd_id = e.fd_id WHERE t.fd_login_name=? and e.fd_is_available=1";

	 public static boolean login(String username, String password){
	        if (username == null || password == null) {
	            return false;
	        }
	        String md5Password = loadMD5Password(username);
	        if(md5Password==null){
	        	return false;
	        }
	        username = username.trim().toLowerCase();
	        if (username.contains("@")) {
	            // Check that the specified domain matches the server's domain
	            int index = username.indexOf("@");
	            username = username.substring(0, index);
	        }
	        if (md5Password.equals(MD5(password).toLowerCase())){
	            return true;
	        }
	        return false;
	 }
	 private static Connection GetDBConnection(String driver, String connectionUrl, String user, String password) {
	        Connection conn = null;
	        try {
	            try {
	                Class.forName(driver).newInstance();
	            } catch (Exception e) {
	                e.printStackTrace();
	                return null;
	            }
	            conn = DriverManager.getConnection(connectionUrl, user, password);
	            return conn;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        }

	    }
	 private static String loadMD5Password(String username) {
	        Connection con = null;
	        PreparedStatement pstmt = null;
	        ResultSet rs = null;
	        String password = null;
	        try {
	        	 InputStream in = new FileInputStream(
		                    new File(EKPAuth.class.getResource("/").getPath()+"ekpOracle.properties"));
		         Properties prop = new Properties();

		         prop.load(in);
	             con = GetDBConnection(prop.getProperty("jdbc.driverClass"),
	            		 prop.getProperty("jdbc.jdbcUrl"),
	            		 prop.getProperty("jdbc.user"),
	            		 prop.getProperty("jdbc.password"));

	            String LOAD_MD5_PASSWORD = LOAD_MD5_PASSWORD_Oracle;
	            pstmt = con.prepareStatement(LOAD_MD5_PASSWORD);
	            pstmt.setString(1, username);
	            rs = pstmt.executeQuery();
	            if (rs.next()) {
	                password = rs.getString(1);
	            } else {
	            	return null;
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        } finally {
	            try {
	            	if(rs!=null){
	            		rs.close();
	            	}
	            } catch (SQLException ex) {
	            }
	            try {
	            	if(pstmt!=null){
	            		pstmt.close();
	            	}
	            } catch (SQLException ex) {
	            }
	            try {
	            	if(con!=null){
	            		con.close();
	            	}
	            } catch (SQLException ex) {

	            }
	        }
	        return password;
	    }
	 private static final String MD5(String s) {
	        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	        try {
	            byte[] btInput = s.getBytes();
	            // 获得MD5摘要算法的 MessageDigest 对象
	            MessageDigest mdInst = MessageDigest.getInstance("MD5");
	            // 使用指定的字节更新摘要
	            mdInst.update(btInput);
	            // 获得密文
	            byte[] md = mdInst.digest();
	            // 把密文转换成十六进制的字符串形式
	            int j = md.length;
	            char str[] = new char[j * 2];
	            int k = 0;
	            for (int i = 0; i < j; i++) {
	                byte byte0 = md[i];
	                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
	                str[k++] = hexDigits[byte0 & 0xf];
	            }
	            return new String(str);
	        } catch (Exception e) {
	            e.printStackTrace();
	            return null;
	        }
	    }

}
